var g_time;
var g_startTime;

function EnemyGenerator(){
    var x = [100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600];
    var y = [40, 400];
    g_time += 1; //this is updated 30 times per 1 second since we are running at 30fps
    var ONE_SEC=30;
    var ONE_MINUTE = 60 * ONE_SEC;
    var num_enemies = 2;

    num_enemies += (g_time/(10 * ONE_SEC));
    if(num_enemies > 15)
        num_enemies = 15; // max enemy count 15

    if(g_time % (ONE_MINUTE/2) == 0){ //add powerups 
        var pups = [png_star, png_hero];
        var tmp = new Enemy(RandChoice(x), RandChoice(y), RandChoice(pups), BounceAround); 
        tmp.damage = -4;
        g_enemyList.push(tmp);
    }

    if(g_time % (ONE_MINUTE) == 0){
        var tmp = new Enemy(RandChoice(x), RandChoice(y), png_spiral, SpawningEnemy); 
        g_enemyList.push(tmp);
    }
    if(g_time % (2*ONE_MINUTE) == 0){
        var tmp = new Enemy(RandChoice(x), RandChoice(y), png_balls, BounceSpawn); 
        g_enemyList.push(tmp);
    }

    if(g_enemyList.length < num_enemies)
    {
        ///add the enemies.
        var t = [
            function(){g_enemyList.push(new Enemy(RandChoice(x), RandChoice(y), png_tris, FollowPlayer));}, 
            function(){g_enemyList.push(new Enemy(RandChoice(x), RandChoice(y), png_hash, BounceAround));}];
        f = RandChoice(t);
        f();
    } 
}

function draw() //game draw cycle.
{
    if(!g_startGame)//dont' start the game unless start is pressed.
        return;
    var radius = 60;
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    g_player.draw();
    for(i in g_enemyList)
    {
        g_enemyList[i].draw();
    }
    //drawing particles on top of everything.
    for(var i in g_particleList){
        g_particleList[i].draw();
    }
    
}


function update() //game update cycle.
{
    if(!g_startGame)//dont' start the game unless start is pressed.
        return;
    EnemyGenerator();
    g_player.update();
    var tmp = new Array();
    for(var i=0; i < g_enemyList.length; i++)
    {
        g_enemyList[i].update();
        if(g_player.isOverlap(g_enemyList[i]))
        {//enemy is in the disintegration range.
            g_enemyList[i].health -= g_player.damage;
            if(g_enemyList[i].health % 5 == 0)
                AddBlast(g_enemyList[i], "rgb(255,0,0)");
        }
        if(g_enemyList[i].health > 0)
        {            
            tmp.push(g_enemyList[i]);
        }
        else
        {//an enemy died.. add score.
            g_player.score += 10;// vary based on enemies.
            //check if we are above the min high score.
            if(!g_player.new_highScore && g_player.score > g_highScores[0])
            {
                //show the player that they made a new high score.
                g_TextMessage.init("New High Score");
                g_player.new_highScore = true;
            }
            if(g_player.health < 100)
                g_player.health += 1; 
        }
    }
    g_enemyList = tmp;
    //check if the game is over.
    if(g_player.health < 0){
        //check the previous scores.
        //set the current score if greater than any high score.
        g_startGame = false;
        //g_pausebutton.style.visibility = "hidden";
        var d = new Date();
        g_mesg.innerHTML='<b>Game Over</b><br/> <table><tr><td>Score:</td><td>'+ g_player.score +
            '</td></tr><tr><td>Time:</td><td>' + (d.getTime() - g_startTime)/1000 +
            ' secs </td></tr></table><br/><b>HighScores:</b><br/>'+ getHighScores() + 
            '<input type="button" value="Restart" onClick="window.location.reload()">';
//            '<input type="button" value="Restart" onClick="LoadingDone();">';
        g_mesg.style.height=300;
        g_mesg.style.width=400;
        g_mesg.style.top=canvas.offsetTop + canvas.height/2 - 150;
        g_mesg.style.left=canvas.offsetLeft + canvas.width/2 - 200;
        g_mesg.style.visibility="visible";
    }
    var tmp = new Array();
    for(var i in g_particleList){
        g_particleList[i].update();
        if(g_particleList[i].live)
            tmp.push(g_particleList[i]);
    }
    g_particleList = tmp;
    //ui handler for showing in game messages.
    g_TextMessage.update();
}
